<template>
  <j-modal
    :title="$t('popup.promotionPriceFormula')"
    :width="width"
    :visible="priceVisible"
    @cancel="priceHandleCancel"
    style="overflow-y: hidden">
    <template slot="footer">
      <a-button @click="priceHandleCancel">{{$t('inPrint.cancel')}}</a-button>
      <a-button type="primary" @click="priceHandleOk">{{$t('popup.determine')}}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
            <a-row class="form-row" :gutter="10">
              <a-col :lg="4" :md="12" :sm="24">
                <a-form-item :labelCol="labelCol">
                  {{$t('popup.promotionPrice')}}=
                </a-form-item>
              </a-col>
              <a-col :lg="7" :md="10" :sm="24">
                <a-form-item :labelCol="labelCol">
                  <a-select :placeholder="$t('popup.selectPriceType')" v-decorator="[ 'valenceSpecies', validatorRules.valenceSpecies]"
                    :dropdownMatchSelectWidth="false" showSearch optionFilterProp="children">
                      <!-- <a-select-option value="采购价">采购价</a-select-option> -->
                      <a-select-option value="6">{{$t('commodityManagement.retailPrice')}}</a-select-option>
                      <a-select-option value="1">{{$t('popup.primaryWholesalePrice')}}</a-select-option>
                      <a-select-option value="2">{{$t('popup.secondaryWholesalePrice')}}</a-select-option>
                      <a-select-option value="3">{{$t('popup.tertiaryWholesalePrice')}}</a-select-option>
                      <a-select-option value="4">{{$t('popup.level4WholesalePrice')}}</a-select-option>
                      <a-select-option value="5">{{$t('popup.level5WholesalePrice')}}</a-select-option>
                      <a-select-option value="7">{{$t('popup.lowestSellingPrice')}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :lg="6" :md="12" :sm="24">
                <a-form-item :labelCol="labelCol">
                  <a-select :placeholder="$t('popup.addSubtractMultiplyDivide')" v-decorator="[ 'algorithm', validatorRules.algorithm]"
                    :dropdownMatchSelectWidth="false" showSearch optionFilterProp="children">
                      <a-select-option value="+">+</a-select-option>
                      <a-select-option value="-">-</a-select-option>
                      <a-select-option value="*">*</a-select-option>
                      <a-select-option value="/">/</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :lg="6" :md="12" :sm="24">
                <a-form-item :labelCol="labelCol">
                    <a-input :placeholder="$t('popup.importValue')" v-decorator="[ 'priceDiscount', validatorRules.priceDiscount ]" />
                </a-form-item>
              </a-col>
                  <!-- <a-col :lg="24" :md="24" :sm="24">
                    <a-form-item :labelCol="labelCol" :wrapperCol="{xs: { span: 24 },sm: { span: 24 }}" label="">
                      <a-textarea :rows="1" placeholder="请输入备注" v-decorator="[ 'remark' ]" style="margin-top:8px;"/>
                    </a-form-item>
                  </a-col> -->
                </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import { getPriceByBarCode } from '@/api/api'
  // import ManyAccountModal from '../dialog/ManyAccountModal'
  // import CustomerModal from '../../system/modules/CustomerModal'
  // import AccountModal from '../../system/modules/AccountModal'
  // import HistoryBillList from '../dialog/HistoryBillList'
  // import { FormTypes } from '@/utils/JEditableTableUtil'
  // import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  // import { BillModalMixin } from '../mixins/BillModalMixin'
  // import { getMpListShort,handleIntroJs } from "@/utils/util"
  // import { BillListMixin } from '../mixins/BillListMixin'
  // import JSelectMultiple from '@/components/jeecg/JSelectMultiple'
  // import JUpload from '@/components/jeecg/JUpload'
  // import JDate from '@/components/jeecg/JDate'
  import Vue from 'vue'
  export default {
    name: "PriceFormulaModal",
    props: {
      visible: {
        type: String | Boolean ,
        required: true
      },
    },
    watch:{
      visible(val, valOld) {
        this.priceVisible = val
      }
    },
    // mixins: [JEditableTableMixin, BillModalMixin, BillListMixin],
    components: {
      // ManyAccountModal,
      // CustomerModal,
      // AccountModal,
      // HistoryBillList,
      // JUpload,
      // JDate,
      // JSelectMultiple,
      // VNodes: {
      //   functional: true,
      //   render: (h, ctx) => ctx.props.vnodes,
      // }
    },
    data () {
      return {
        title:this.$t('setailManagement.operation'),
        width: '500px',
        form: this.$form.createForm(this),
        queryParam: {
          organId: '所有客户',
          warehouse: '所有仓库',
        },
        moreStatus: false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        priceVisible: this.visible,
        priceList: [],
        // visible: false,
        operTimeStr: '',
        prefixNo: 'CXTJ',
        fileList:[],
        model: {},
        valenceSpecies: '6',
        arrList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        refKeys: ['materialDataTable', 'customerDataTable', 'warehouseDataTable'],
        activeKey: 'materialDataTable',
        confirmLoading: false,
        validatorRules:{
          valenceSpecies:{
            rules: [
              { required: true, message: this.$t('popup.pleaseEnterPriceCategory')+'!' }
            ]
          },
          algorithm:{
            rules: [
              { required: true, message: this.$t('popup.pleaseSelectAdditionSubtraction')+'!' }
            ]
          },
          warehouse:{
            rules: [
              { required: true, message: this.$t('setailManagement.warehouse')+'!' }
            ]
          }
        },
        url: {
          add: '/depotHead/addDepotHeadAndDetail',
          edit: '/depotHead/updateDepotHeadAndDetail',
          detailList: '/depotItem/getDetailList'
        }
      }
    },
    created () {
    },
    methods: {
      handleChange(value) {
        this.valenceSpecies = value
        console.log('this.valenceSpecies')
        console.log(this.valenceSpecies)
        // this.priceFormula()
      },
      priceHandleCancel() {
        this.priceVisible = false
        this.$emit('visibleType', this.priceVisible);
      },
      priceHandleOk() {
        this.priceFormula()
        // if (this.priceList.length > 0) {
          // console.log(this.priceList)
          // this.$emit('priceList', this.priceList);
          // this.priceHandleCancel()
        // }
      },
      priceFormula() {
        this.form.validateFields((err, values) => {
        let param = {
                mpList: '',
                barCode: this.arrList.join(','),
                grade: this.valenceSpecies,
                prefixNo: 'CXTJ'
              }
              getPriceByBarCode(param).then((res) => {
                  if (res && res.code === 200) {
                    res.data.forEach(item => {
                      if (values.algorithm == '*') {
                        console.log(Number(item.billPrice))
                        console.log(Number(values.priceDiscount))
                        item.specialPrice = Number(item.billPrice) * Number(values.priceDiscount)
                      } else if(values.algorithm == '+') {
                        item.specialPrice = Number(item.billPrice) + Number(values.priceDiscount)
                      } else if(values.algorithm == '-') {
                        item.specialPrice = Number(item.billPrice) - Number(values.priceDiscount)
                      } else if(values.algorithm == '/') {
                        item.specialPrice = Number(item.billPrice) / Number(values.priceDiscount)
                      }
                      console.log(item.specialPrice)
                      item.discount = values.priceDiscount
                    })
                    this.priceList = res.data
                    this.$emit('priceList', this.priceList);
                    this.priceHandleCancel()
                  }
              })
          })
      },
      //调用完edit()方法之后会自动调用此方法
      editAfter(val) {
        // this.billStatus = '0'
        // this.changeColumnHide()
        // if (this.action === 'add') {
        //   this.addInit(this.prefixNo)
        //   this.personList.value = ''
        //   this.fileList = []
        //   this.$nextTick(() => {
        //     handleIntroJs(this.prefixNo, 1)
        //   })
        // } else {
        //   this.model.operTime = this.model.operTimeStr
        //   if(this.model.accountId == null) {
        //     this.model.accountId = 0
        //     this.manyAccountBtnStatus = true
        //     this.accountIdList = this.model.accountIdList
        //     this.accountMoneyList = this.model.accountMoneyList
        //   } else {
        //     this.manyAccountBtnStatus = false
        //   }
        //   this.personList.value = this.model.salesMan
        //   
        // this.form.resetFields();
          this.valenceSpecies = '6'
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,'priceDiscount','algorithm','valenceSpecies'))
            this.form.setFieldsValue({
              algorithm: '*',
              valenceSpecies: '6',
              priceDiscount: '0.2',
            })
            val.forEach(item => {
              this.arrList.push(item.meId)
            })
            // this.priceFormula()
          });
          
        //   // 加载子表数据
        //   let params = {
        //     headerId: this.model.id,
        //     mpList: getMpListShort(Vue.ls.get('materialPropertyList'))  //扩展属性
        //   }
        //   let url = this.readOnly ? this.url.detailList : this.url.detailList;
        //   this.requestSubTableData(url, params, this.materialTable);
        // }
        // //复制新增单据-初始化单号和日期
        // if(this.action === 'copyAdd') {
        //   this.model.id = ''
        //   this.model.tenantId = ''
        //   this.copyAddInit(this.prefixNo)
        // }
        // this.initCustomer()
        // this.initSalesman()
        // this.initAccount()
      },
      //提交单据时整理成formData
      // classifyIntoFormData(allValues) {
      //   let totalPrice = 0
      //   let billMain = Object.assign(this.model, allValues.formValue)
      //   let detailArr = allValues.tablesValue[0].values
      //   billMain.type = '其它'
      //   billMain.subType = '促销特价订单'
      //   billMain.defaultNumber = billMain.number
      //   for(let item of detailArr){
      //     item.depotId = '' //订单不需要仓库
      //     totalPrice += item.allPrice-0
      //   }
      //   billMain.totalPrice = totalPrice
      //   if(billMain.accountId === 0) {
      //     billMain.accountId = ''
      //   }
      //   billMain.accountIdList = this.accountIdList.length>0 ? JSON.stringify(this.accountIdList) : ""
      //   billMain.accountMoneyList = this.accountMoneyList.length>0 ? JSON.stringify(this.accountMoneyList) : ""
      //   if(this.fileList && this.fileList.length > 0) {
      //     billMain.fileName = this.fileList
      //   } else {
      //     billMain.fileName = ''
      //   }
      //   if(this.model.id){
      //     billMain.id = this.model.id
      //   }
      //   billMain.salesMan = this.personList.value
      //   billMain.status = this.billStatus
      //   return {
      //     info: JSON.stringify(billMain),
      //     rows: JSON.stringify(detailArr),
      //   }
      // },
      // handleHistoryBillList() {
      //   let organId = this.form.getFieldValue('organId')
      //   this.$refs.historyBillListModalForm.show('其它', '销售订单', '客户', organId);
      //   this.$refs.historyBillListModalForm.disableSubmit = false;
      // },
    }
  }
</script>
<style scoped>

</style>